<%@ page language="java" contentType="text/html; charset=UTF-8"
	pageEncoding="UTF-8"%>
<%@ taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core"%>
<%@ taglib prefix="s" uri="http://www.springframework.org/tags"%>
<%@ taglib uri="http://java.sun.com/jsp/jstl/functions" prefix="fn"%>
<%@ taglib prefix="fmt" uri="http://java.sun.com/jsp/jstl/fmt"%>
<%@ taglib prefix="security"
	uri="http://www.springframework.org/security/tags"%>
<security:authentication property="principal" var="myAccount" />
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<%@ page isELIgnored="false"%>
<title>Index</title>
</head>
<body>
	<h1>
		Forum
		<c:out value="${groupe.name}" />
	</h1>
	<table id="viewSujetForum">
		<thead>
			<tr>
				<th class="auteurForum">Auteur</th>
				<th class="contentForum"><c:out value="${sujet.titre}" /> - <fmt:formatDate
						value="${sujet.dateCreation}" pattern="dd-MMM-yyyy à HH:mm" /></th>
			</tr>
		</thead>
		<tbody>
			<tr>
				<td class="auteurForumC">
					<c:choose>
						<c:when test="${sujet.createur.srcAvatar == null}">
							<img class="avatarForum"
								src="${pageContext.request.contextPath}/resources/img/no_avatar.jpg" />
						</c:when>
						<c:otherwise>
							<img class="avatarForum"
								src="${pageContext.request.contextPath}/resources/upload/user/avatar/${sujet.createur.srcAvatar}" />
						</c:otherwise>
					</c:choose> 
					<p class="ident">
					
					<c:out value="${sujet.createur.firstName}" /> 
					<c:out value="${sujet.createur.lastName}" />
					</p>
				</td>
				<td>
					${sujet.contenu}
					<c:if test="${not empty sujet.dateModification}">
						<p> --- Message édité --- <fmt:formatDate value="${sujet.dateModification}" pattern="dd-MMM-yyyy à HH:mm" />	</p>
					</c:if>
					<c:if test="${not empty sujet.dateModeration}">
						<p> --- Message modéré --- <fmt:formatDate value="${sujet.dateModeration}" pattern="dd-MMM-yyyy à HH:mm" />	</p>
					</c:if>					
					<!--   options -->
					<div class="actionForum">
						<c:if test="${sujet.createur.idUser == user.idUser}">
							<a href="${pageContext.request.contextPath}/forum/${groupe.slug}/${sujet.idSujet}/edit">						
							<img id="buttonEdit"
								src="${pageContext.request.contextPath}/resources/img/pen.png"
								title="Editer le sujet" /></a>
						</c:if>	
						<security:authorize access="hasRole('ROLE_ADMIN')">
							<a href="${pageContext.request.contextPath}/forum/${groupe.slug}/${sujet.idSujet}/moderer">	
							<img
								src="${pageContext.request.contextPath}/resources/img/moderate.png"
								title="Modérer le sujet" /></a>
							<c:choose>
								<c:when test="${!sujet.isClose}">
									<a href="${pageContext.request.contextPath}/forum/${groupe.slug}/${sujet.idSujet}/lock">
									<img
										src="${pageContext.request.contextPath}/resources/img/lock.png"
										title="Vérrouiller le sujet" /></a>								
								</c:when>
								<c:otherwise>
									<a href="${pageContext.request.contextPath}/forum/${groupe.slug}/${sujet.idSujet}/unlock">
									<img
										src="${pageContext.request.contextPath}/resources/img/key.png"
										title="Dévérrouiller le sujet" />	</a>								
								</c:otherwise>
							</c:choose> 
								<a href="${pageContext.request.contextPath}/forum/${groupe.slug}/${sujet.idSujet}/delete">
								<img
								src="${pageContext.request.contextPath}/resources/img/trash.png"
								title="Supprimer le sujet" />		</a>							
						</security:authorize> 	
					</div>
				</td>
			</tr>
	<!-- 	Liste des réponses -->
   	<c:forEach var="reponse" items="${reponses}">	
 		<tr>
				<td class="auteurForumC">
					<c:choose>
						<c:when test="${reponse.postedBy.srcAvatar == null}">
							<img class="avatarForum"
								src="${pageContext.request.contextPath}/resources/img/no_avatar.jpg" />
						</c:when>
						<c:otherwise>
							<img class="avatarForum"
								src="${pageContext.request.contextPath}/resources/upload/user/avatar/${reponse.postedBy.srcAvatar}" />
						</c:otherwise>
					</c:choose> 
					<p class="ident">
					
					<c:out value="${reponse.postedBy.firstName}" /> 
					<c:out value="${reponse.postedBy.lastName}" />
					</p>
				</td>
				<td><p class="dateRepForum"><fmt:formatDate value="${reponse.dateLastModification}" pattern="dd-MMM-yyyy à HH:mm" /></p>
					${reponse.contenu}
					<c:if test="${not empty reponse.dateModification}">
						<p> --- Message édité --- <fmt:formatDate value="${reponse.dateModification}" pattern="dd-MMM-yyyy à HH:mm" />	</p>
					</c:if>
					<c:if test="${not empty reponse.dateModeration}">
						<p> --- Message modéré --- <fmt:formatDate value="${reponse.dateModeration}" pattern="dd-MMM-yyyy à HH:mm" />	</p>
					</c:if>						
					<!--   options -->
					<div class="actionForum">
						<c:if test="${reponse.postedBy.idUser == user.idUser}">	
							<a href="${pageContext.request.contextPath}/forum/${groupe.slug}/editmsg/${reponse.idReponse}">					
							<img id="buttonEdit"
								src="${pageContext.request.contextPath}/resources/img/pen.png"
								title="Editer le sujet" /></a>
						</c:if>	
						<security:authorize access="hasRole('ROLE_ADMIN')">
							<a href="${pageContext.request.contextPath}/forum/${groupe.slug}/moderermsg/${reponse.idReponse}">	
							<img
								src="${pageContext.request.contextPath}/resources/img/moderate.png"
								title="Modérer le sujet" /></a>
							<c:choose>
								<c:when test="${!sujet.isClose}">
									<a href="${pageContext.request.contextPath}/forum/${groupe.slug}/${sujet.idSujet}/lock">
									<img
										src="${pageContext.request.contextPath}/resources/img/lock.png"
										title="Vérrouiller le sujet" /></a>								
								</c:when>
								<c:otherwise>
									<a href="${pageContext.request.contextPath}/forum/${groupe.slug}/${sujet.idSujet}/unlock">
									<img
										src="${pageContext.request.contextPath}/resources/img/key.png"
										title="Dévérrouiller le sujet" />	</a>								
								</c:otherwise>					
							</c:choose>
								<a href="${pageContext.request.contextPath}/forum/${groupe.slug}/${sujet.idSujet}/delete/${reponse.idReponse}">
								<img
								src="${pageContext.request.contextPath}/resources/img/trash.png"
								title="Supprimer la réponse" />		</a>								
						</security:authorize> 	
					</div>
				</td>
			</tr>  	
   	</c:forEach>
	</tbody>
</table>   	
	<!-- 	Ajouter une réponse -->	
    <c:if test="${!sujet.isClose}">	
    	<form action="ajoutReponse" method="post" id="addRepForum"> 
    		<input type="hidden" name="idSujet" value="${sujet.idSujet}"/>
    		<input type="submit" name="envoyer" value="Ajouter une réponse" class="btn btn-lg btn-primary "/>
    	</form>
	</c:if>
<a href="<c:url value="/forum/${groupe.slug}"/>" class="btn btn-lg btn-primary ">Retourner au forum</a>		
</body>
</html>